<!-- Licensed under a BSD license. See license.html for license -->
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <title>WebGL - 2D Rectangle</title>
    <link type="text/css" href="../resources/webgl-tutorials.css" rel="stylesheet" />
</head>

<body>
    <div id="info">
        <a href="https://webglfundamentals.org/webgl/lessons/zh_cn/webgl-fundamentals.html" target="_blank">WebGL
            基础概念</a>
    </div>
    <canvas id="c"></canvas>
</body>
<!--
for most samples webgl-utils only provides shader compiling/linking and
canvas resizing because why clutter the examples with code that's the same in every sample.
See http://webglfundamentals.org/webgl/lessons/webgl-boilerplate.html
and http://webglfundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html
for webgl-utils, m3, m4, and webgl-lessons-ui.
-->
<script src="../resources/webgl-utils.js"></script>
<!-- vertex shader -->
<script id="2d-vertex-shader" type="notjs">
  attribute vec4 a_position;

  uniform vec2 u_resolution;

  void main() {
     // 从像素坐标转换到 0.0 到 1.0
     vec2 zeroToOne = a_position.xy / u_resolution;

     // 再把 0->1 转换 0->2
     vec2 zeroToTwo = zeroToOne * 2.0;

     // 把 0->2 转换到 -1->+1 (裁剪空间)
     vec2 clipSpace = zeroToTwo - 1.0;

     gl_Position = vec4(clipSpace, 0, 1);
  }
</script>
<!-- fragment shader -->
<script id="2d-fragment-shader" type="notjs">
  precision mediump float;

  void main() {
    gl_FragColor = vec4(1, 0, 0.5, 1); // return redish-purple
  }
</script>
<script>
    "use strict";

    function main() {
        // 获取WebGL上下文
        var canvas = document.getElementById("c");
        var gl = canvas.getContext("webgl");
        if (!gl) {
            return;
        }

        // 创建着色程序
        var program = webglUtils.createProgramFromScripts(gl, ["2d-vertex-shader", "2d-fragment-shader"]);

        // 从着色程序中找到a_position属性值所在的位置
        var positionAttributeLocation = gl.getAttribLocation(program, "a_position");

        // 从着色程序中找到u_resolution属性值所在的位置
        var resolutionUniformLocation = gl.getUniformLocation(program, "u_resolution");

        // 创建存放三个2维裁剪空间点的缓存
        var positionBuffer = gl.createBuffer();

        // 绑定位置信息缓冲（下面的绑定点就是ARRAY_BUFFER）
        gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);

        var positions = [
            10, 20,
            80, 20,
            10, 30,
            10, 30,
            80, 20,
            80, 30,
        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);

        webglUtils.resizeCanvasToDisplaySize(gl.canvas);

        // 告诉WebGL怎样把提供的gl_Position裁剪空间坐标对应到画布像素坐标（屏幕空间）
        gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);

        // 清空画布
        gl.clearColor(0, 0, 0, 0);
        gl.clear(gl.COLOR_BUFFER_BIT);

        // 告诉它用我们之前写好的着色程序（一个着色器对）
        gl.useProgram(program);

        // 启用对应属性
        gl.enableVertexAttribArray(positionAttributeLocation);

        // 将绑定点绑定到缓冲数据（positionBuffer）
        gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);

        // 告诉属性怎么从positionBuffer中读取数据 (ARRAY_BUFFER)
        var size = 2;          // 每次迭代运行提取两个单位数据
        var type = gl.FLOAT;   // 每个单位的数据类型是32位浮点型
        var normalize = false; // 不需要归一化数据
        var stride = 0;        // 0 = 移动单位数量 * 每个单位占用内存（sizeof(type)）每次迭代运行运动多少内存到下一个数据开始点
        var offset = 0;        // 从缓冲起始位置开始读取
        gl.vertexAttribPointer(
            positionAttributeLocation, size, type, normalize, stride, offset);

        // 设置全局变量 分辨率
        gl.uniform2f(resolutionUniformLocation, gl.canvas.width, gl.canvas.height);

        // 绘制
        var primitiveType = gl.TRIANGLES;
        var offset = 0;
        var count = 6;
        gl.drawArrays(primitiveType, offset, count);
    }

    main();
</script>

</html>